package gold.digger;


import gold.vo.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC863 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        // 用map记录每个节点的父节点
        private Map<TreeNode, TreeNode> parents = new HashMap<>();

        private Set<TreeNode> used = new HashSet<>();

        private TreeNode targetNode;

        // 找到目标节点后以目标节点为开始位置向三个方向蔓延
        public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
            find(root, null, target);
            List<Integer> res = new LinkedList<>();
            dfs(targetNode, res, K);
            return res;
        }

        private void find(TreeNode root, TreeNode parent, TreeNode target) {
            if (null == root) {
                return;
            }
            if (root.val == target.val) {
                targetNode = root;
            }
            parents.put(root, parent);
            find(root.left, root, target);
            find(root.right, root, target);
        }

        private void dfs(TreeNode root, List<Integer> collector, int distance) {
            if (root != null && !used.contains(root)) {
                // 标记为已访问
                used.add(root);
                if (distance <= 0) {
                    collector.add(root.val);
                    return;
                }
                dfs(root.left, collector, distance - 1);
                dfs(root.right, collector, distance - 1);
                dfs(parents.get(root), collector, distance - 1);
            }
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC863 an = new LC863();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
